import threading
import queue
from models.model import generate_sensor_readings, data_ranges, validate_sensor_data
from ui.window import LogWindow

class Controller:
    def __init__(self):
        self.log_queue = queue.Queue()
        self.pause_flag = [False]
        self.running_flag = [True]
        self.threads = []

    def start(self):
        # 启动数据生成线程
        for i in range(5):  # 启动5个线程作为示例
            sensor_thread = threading.Thread(
                target=generate_sensor_readings,
                args=("output/sensor_output.csv", self.log_queue, f"Device_{i}", self.running_flag, self.pause_flag),
                name=f"Device_Thread_{i}"
            )
            sensor_thread.start()
            self.threads.append(sensor_thread)

        # 启动日志窗口
        self.log_window = LogWindow(self.log_queue, self.pause_flag)
        self.log_window.root.mainloop()

    def stop(self):
        self.running_flag[0] = False
        for thread in self.threads:
            thread.join()

if __name__ == "__main__":
    controller = Controller()
    controller.start()